﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading.Tasks;
using Frogs.Stability.CircuitBreaker.Exceptions;

namespace Frogs.Stability.CircuitBreaker.Behaviors.Actions
{
    class HalfOpenBehavior : IActionCircuitBehavior
    {
        public void DoAction(Action action)
        {
            try
            {
                action();
				this._executionResult.OnNext(Behaviors.ExecutionResult.Success);
            }
            catch (Exception excp)
            {
				this._executionResult.OnNext(Behaviors.ExecutionResult.Fail);
				throw new HalfOpenCircuitBreakerException("Half-open circuit breaker failed, see inner exception for details", excp);
            }
        }

        public CircuitState GetStatus()
        {
            return CircuitState.HalfWay;
        }

		Subject<ExecutionResult> _executionResult = new Subject<ExecutionResult>();
		public IObservable<ExecutionResult> ExecutionResult { get { return this._executionResult; } }
    }
}
